<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="./zepto.1.1.6.min.js"></script>
    <title>跑步机</title>
    <script>
        var activityId = 24;
    </script>
</head>
<body style="display: -webkit-box;">
<div class="container" style="width:850px;">
    player: <input type="text" id="player" value="1">

    <div class="row">
        <h5>online</h5>
        <div>username: <input type="text" id="username" value="手动"> <input type="button" id="qrcode" value="扫码"></div>
    </div>
    <div class="row">
        <h5>app</h5>
        <div style="visibility: hidden;"><input type="button" id="listen_code" value="app监听qrcode"></div>
        <div><input type="button" id="begin" value="开始"></div>
        <div><input type="button" id="stop" value="终止"></div>
        <!--<div><input type="button" id="getresult" value="获取信息"></div>-->
        <div> points: <input type="text" class="points" value="1"> <input type="button" id="result" value="提交信息"></div>
    </div>

    <div style="visibility: hidden;">
        <h5>offline</h5>
        <div><input type="button" id="pbj" value="check"></div>
    </div>
</div>


<div id="log" style="-webkit-box-flex: 1;min-width: 400px;">
    <h3 id="end">LOG</h3>
</div>


<script>
    console.log = function () {
//        console.log(arguments);
        var s = '';
        for (var n = 0; n < arguments.length; n++) {
            s += arguments[n];
            s += " : ";
        }
//        console.log(s);
        $("<div style='white-space:nowrap;'>" + s + "</div>").insertAfter("#end");
    };

    var timer1, timer2;

    $("#qrcode").on("click", function () {
        var self = this;
        $.ajax({
            url: "http://adidas.weixinzjit.com/index/online/setuser",
            type: "POST",
            dataType: "json",
            data: {
                "participantId": 1,
                "activityId": activityId,
                "clientKey": "sd325SFV3sd23g2",
                "username": $("#username").val(),
                player: $("#player").val()
            },
            success: function (data) {
                console.log("扫码", JSON.stringify(data));

//                $("#listen_code").trigger("click");
            }
        });
    });

    $("#begin").on("click", function () {
        var self = this;

        $.ajax({
            url: "http://adidas.weixinzjit.com/index/app/begin",
            type: "POST",
            dataType: "json",
            data: {
                "activityId": activityId,
                player: $("#player").val()
            },
            success: function (data) {
                console.log('开始', JSON.stringify(data));
//                $("#getresult").trigger("click");
            }
        });
    });

    $("#stop").on("click", function () {
        var self = this;
        $.ajax({
            url: "http://adidas.weixinzjit.com/index/app/stop",
            type: "POST",
            dataType: "json",
            data: {
                "activityId": activityId,
                player: $("#player").val()
            },
            success: function (data) {
                console.log('停止', JSON.stringify(data));
                if (timer1) clearInterval(timer1);
                if (timer2) clearInterval(timer2);
            }
        });
    });

    $("#getresult").on("click", function () {
        var self = this;

        timer1 = setInterval(function () {
            $.ajax({
                url: "http://adidas.weixinzjit.com/index/app/getresult",
                type: "POST",
                dataType: "json",
                data: {
                    "activityId": activityId,
                    player: $("#player").val()
                },
                success: function (data) {
                    console.log('获取信息', JSON.stringify(data));

                    if (!data.errno)
                        clearInterval(timer1);
                    else {
                        $("#result").trigger("click");
                    }
                }
            });
        }, 3000);
    });

    $("#result").on("click", function () {
        var self = this;

        $.ajax({
            url: "http://adidas.weixinzjit.com/index/app/result",
            type: "POST",
            dataType: "json",
            data: {
                "activityId": activityId,
                "points": $(self).parent().find(".points").val(),
                player: $("#player").val()
            },
            success: function (data) {
                console.log('结果', JSON.stringify(data));
            }
        });
    });

    $("#listen_code").on("click", function () {
        var self = this;

        timer2 = setInterval(function () {
            $.ajax({
                url: "http://adidas.weixinzjit.com/index/app/getqueue",
                type: "POST",
                dataType: "json",
                data: {
                    "activityId": activityId,
                    player: $("#player").val()
                },
                success: function (data) {
                    console.log('app监听扫码', JSON.stringify(data));
                    if (!data.errno) {
                        clearInterval(timer2);
                    }
                }
            })
        }, 3000);
    });


</script>
<script>
    $("#pbj").on("click", function () {
        var timer = setInterval(function () {
            $.ajax({
                url: "http://adidas.weixinzjit.com/index/offline/GuinnessWorldRecords/check",
                type: "POST",
                data: {},
                success: function (data) {
                    console.log("check", JSON.stringify(data));
                }
            });
        }, 3000);
    });

    //    $("#pbj").trigger("click");
</script>
</body>
</html>